package it.gestionedocumentale.struts.action.admin.utente;

import it.gestionedocumentale.model.Gruppo;
import it.gestionedocumentale.model.Utente;
import it.gestionedocumentale.spring.service.MyServiceFactory;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class PrepareInsertUtenteFormAction extends Action {
	private static Log log = LogFactory.getLog(PrepareInsertUtenteFormAction.class);
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Utente utenteSession = (Utente) request.getSession().getAttribute("utenteSession");
		if(utenteSession == null) {
			return mapping.findForward("notInSession");
		}
		if(!utenteSession.getGruppo().getCodGruppo().equals("ADMIN")) {
			return mapping.findForward("notAllowed");
		}
		
		List<Gruppo> listaGruppi = null;
		
		try {
			listaGruppi = MyServiceFactory.getGruppoService().findAll();
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			return mapping.findForward("error");
		}

		request.setAttribute("listaGruppi", listaGruppi);
		return mapping.findForward("insertUtenteForm");
		
	}

}
